package controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import thread.CopyThread;
import utils.*;

import java.net.URL;
import java.util.ResourceBundle;

public class RightPanelController implements Initializable {
    public static RightPanelController controller;
    @FXML
    private TextField nameIpt;
    @FXML
    private TextField userNameIpt;
    @FXML
    private TextField passwordIpt;
    @FXML
    private TextField svnPathIpt;
    @FXML
    private TextField localPathIpt;
    @FXML
    private TextField classesPathIpt;
    @FXML
    private TextField outPathIpt;
    @FXML
    private TextArea versionsIpt;
    @FXML
    public Button copyBtn;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        String name = resourceBundle.getString("name");
        ConfigBean bean = Global.getConfigBean(name);
        nameIpt.setText(name);
        if(bean!=null){
            userNameIpt.setText(bean.getUserName());
            passwordIpt.setText(bean.getPassword());
            svnPathIpt.setText(bean.getSvnPath());
            localPathIpt.setText(bean.getLocalPath());
            classesPathIpt.setText(bean.getClassesPath());
            outPathIpt.setText(bean.getOutPath());
        } else {
            bean = new ConfigBean();
            bean.setName(name);
            Global.configBeanMap.put(name, bean);
            Global.setCurConfigBean(bean);
        }
        controller = this;
    }

    public void saveCfg(MouseEvent mouseEvent) {
        String name = StringUtils.trim(nameIpt.getText());
        String userName = StringUtils.trim(userNameIpt.getText());
        String password = StringUtils.trim(passwordIpt.getText());
        String svnPath = StringUtils.trim(svnPathIpt.getText());
        String localPath = StringUtils.trim(localPathIpt.getText());
        String classesPath = StringUtils.trim(classesPathIpt.getText());
        String outPath = StringUtils.trim(outPathIpt.getText());

        if(StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(userName) || StringUtils.isNullOrEmpty(password)
                || StringUtils.isNullOrEmpty(svnPath) || StringUtils.isNullOrEmpty(localPath)
                || StringUtils.isNullOrEmpty(classesPath) || StringUtils.isNullOrEmpty(outPath)){

            MonologUtils.alert("请填写完整!");
            return;
        }

        ConfigBean bean = new ConfigBean();
        bean.setName(name);
        bean.setUserName(userName);
        bean.setPassword(password);
        bean.setSvnPath(svnPath);
        bean.setLocalPath(localPath);
        bean.setClassesPath(classesPath);
        bean.setOutPath(outPath);
        Global.saveCfg(bean);

    }

    public void copyFile(MouseEvent mouseEvent) {

        copyBtn.setText("拷贝中");
        copyBtn.setDisable(true);

        String versions = StringUtils.trim(versionsIpt.getText());
        Thread thread = new Thread(new CopyThread(versions, new ACallBack() {
            @Override
            public void exe(Object obj) throws Exception {
                copyBtn.setText("拷贝");
                copyBtn.setDisable(false);
                switch ((int) obj){
                    case 1: MonologUtils.alert("拷贝完成");return;
                    case -1: MonologUtils.alert("拷贝失败");return;
                    case -2: MonologUtils.alert("版本号错误");return;
                    case -3: MonologUtils.alert("请填写版本号");return;
                }
            }
        }));
        thread.start();
    }

    public void showMsg(){

    }
}
